package structures;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GridTableColumnValues {
	
	private List<String> tableNames;
	private Map <String, List<String>> tableColumnNames;
	private Map <String, Map<String, Map<Integer, String>>> tableColumnValues;
	
	private Map <String, Integer> tableMaximumValueCount;
	
	public GridTableColumnValues() {
		tableNames = new ArrayList<String>();
		tableColumnNames = new HashMap <String, List<String>>();
		tableColumnValues = new HashMap<String, Map<String, Map<Integer, String>>>();
		tableMaximumValueCount = new HashMap<String, Integer>();
	}
	
	public void addTable(String tableName){
		if(!tableColumnValues.containsKey(tableName)){
			tableNames.add(tableName);
			tableColumnNames.put(tableName, new ArrayList<String>());
			tableColumnValues.put(tableName, new HashMap<String, Map<Integer, String>>());
		}
	}
	
	public void addTableColumn(String tableName, String columnName){
		addTable(tableName);
		if(!tableColumnNames.get(tableName).contains(columnName)){
			tableColumnNames.get(tableName).add(columnName);
		}
		if(!tableColumnValues.get(tableName).containsKey(columnName)){
			tableColumnValues.get(tableName).put(columnName, new HashMap<Integer, String>());
		}
	}
	public void addTableColumnValue(String tableName, String columnName, Integer lineNumber, String value){
		addTableColumn(tableName, columnName);
		Integer maximumValueCount = tableMaximumValueCount.get(tableName);
		if(maximumValueCount == null)
			maximumValueCount = 0;
		if(lineNumber + 1 > maximumValueCount){
			maximumValueCount = lineNumber + 1;
		}
		tableMaximumValueCount.put(tableName, maximumValueCount);
		int k = 0;
		while(k < lineNumber){
			if(tableColumnValues.get(tableName).get(columnName).get(k) == null)
				tableColumnValues.get(tableName).get(columnName).put(k, "");
			k++;
		}
		tableColumnValues.get(tableName).get(columnName).put(lineNumber, value);
	}
	public void finalize(){
		for (String tableName : tableNames) {
			Integer maximumValueCount = tableMaximumValueCount.get(tableName);
			for (String columnName : tableColumnNames.get(tableName)) {
				if(tableColumnValues.get(tableName).get(columnName).size() < maximumValueCount){
					int i = 0;
					while(i < maximumValueCount){
						if(tableColumnValues.get(tableName).get(columnName).get(i) == null)
							tableColumnValues.get(tableName).get(columnName).put(i, "");
						i++;
					}
				}
			}
		}
	}
	
	public String getTableColumnLineValue(String tableName, String columnName, Integer lineNumber){
		return tableColumnValues.get(tableName).get(columnName).get(lineNumber);
	}

	public Set<Integer> getTableColumnLines(String tableName, String columnName){
		return tableColumnValues.get(tableName).get(columnName).keySet();
	}

	public List<String> getTableNames() {
		return tableNames;
	}

	public List<String> getTableColumns(String tableName) {
		return tableColumnNames.get(tableName);
	}
}
